package com.qupai.controller.user;

import com.qupai.dto.PtOrdersPaymentDTO;
import com.qupai.dto.PtOrdersSubmitDTO;
import com.qupai.result.PageResult;
import com.qupai.result.Result;
import com.qupai.service.PtOrderService;
import com.qupai.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userPtOrderController")
@RequestMapping("/user/ptorder")
@Tag(name = "普通-C端-订单相关接口")
@Slf4j
@RequiredArgsConstructor
public class PtOrderController {

    private final PtOrderService ptOrderService;

    @PostMapping("/submit")
    @Operation(summary = "用户下单")
    public Result<PtOrderSubmitVO> submit(@RequestBody PtOrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单，参数为:{}",ordersSubmitDTO);
        PtOrderSubmitVO orderSubmitVO=ptOrderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @PutMapping("/payment")
    @Operation(summary = "订单支付")
    public Result<PtOrderPaymentVO> payment(@RequestBody PtOrdersPaymentDTO ordersPaymentDTO) {
        log.info("订单支付：{}", ordersPaymentDTO);
        PtOrderPaymentVO orderPaymentVO = ptOrderService.payment(ordersPaymentDTO);
        log.info("订单支付结果：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单查询
     *
     * @param page 页码
     * @param pageSize 每页页数
     * @param status   订单状态 1待付款 2待接单 3已接单 4已完成 5已取消
     * @return 返回的结果
     */
    @GetMapping("/historyOrders")
    @Operation(summary = "历史订单查询")
    public Result<PageResult> page(Long page, Long pageSize, Integer status) {
        PageResult pageResult = ptOrderService.pageQueryUser(page, pageSize, status);
        return Result.success(pageResult);
    }

    @GetMapping("/orderDetail/{id}")
    @Operation(summary = "查询订单详情")
    public Result<PtOrderVO> details(@PathVariable("id") Long id) {
        PtOrderVO orderVO = ptOrderService.details(id);
        return Result.success(orderVO);
    }

    @PutMapping("/cancel/{id}")
    @Operation(summary = "取消订单")
    public Result<String> cancel(@PathVariable("id") Long id) {
        ptOrderService.userCancelById(id);
        return Result.success();
    }

    @PostMapping("/repetition/{id}")
    @Operation(summary = "再来一单")
    public Result<String> repetition(@PathVariable Long id) {
        ptOrderService.repetition(id);
        return Result.success();
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "根据pt订单ids集合删除订单")
    public Result<String> deleteByIds(@RequestBody List<Long> ids) {
        ptOrderService.removeBatchByIds(ids);
        return Result.success();
    }

}
